package com.zuipin.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zuipin.entity.BaseRegion;
import com.zuipin.mapper.BaseRegionMapper;
import com.zuipin.service.IBaseRegionService;
import com.zuipin.util.Pagination;
import com.zuipin.vo.BaseRegionVo;

/**
 * 基础资料-地区服务实现
 * 
 * @ClassName: BaseRegionSer5viceImpl
 * @Description: TODO
 * @author  robot
 * @date 2017年04月257日 08:30:25
 */
@Service
public class BaseRegionServiceImpl implements IBaseRegionService {
	
	@Resource
	private BaseRegionMapper baseRegionMapper;
	
	@Override
	public BaseRegion findById( Integer id) {
		return baseRegionMapper.findById(id);
	}
	
	@Override
	public void insert(BaseRegion baseRegion) {
		baseRegionMapper.insert(baseRegion);
	}
	
	@Override
	public void update(BaseRegion baseRegion) {
		baseRegionMapper.update(baseRegion);
	}
	
	@Override
	public void delete( Integer id) {
		baseRegionMapper.delete(id);
		
	}
	
	@Override
	public List<BaseRegion> seachBaseRegion(BaseRegion baseRegion, Pagination page) {
		return baseRegionMapper.seachBaseRegion(baseRegion, page);
	}

	@Override
	public BaseRegion findByCode(String code) {
		return baseRegionMapper.findByCode(code);
	}

	@Override
	public List<BaseRegionVo> findByParentCode(String parentCode) {
		return baseRegionMapper.findByParentCode(parentCode);
	}


	@Override
	public List<BaseRegion> findAll() {
		return baseRegionMapper.findAll();
	}

	@Override
	public List<BaseRegion> findByLevel(Integer level) {
		return baseRegionMapper.findByLevel(level);
	}

	@Override
	public List<BaseRegionVo> findRegionTree(Integer regionLevel) {
		List<BaseRegion> baseProvs=findByLevel(regionLevel);
		List<BaseRegionVo> regionVos=new ArrayList<>();
		for (BaseRegion baseRegion : baseProvs) {
			BaseRegionVo vo = regionToVo(baseRegion);
			regionVos.add(vo);
		}
		return regionVos;
	}

	/**
	 * @功能描述：
	 * @作者 : cjj 
	 * @创建时间 : 2017年4月25日下午4:03:41
	 * @param baseRegion
	 * @return
	*/
	private BaseRegionVo regionToVo(BaseRegion baseRegion) {
		BaseRegionVo vo=new BaseRegionVo();
		String code = baseRegion.getCode();
		vo.setCode(code);
		vo.setId(baseRegion.getId());
		vo.setParentCode(baseRegion.getParentCode());
		vo.setRegionLevel(baseRegion.getRegionLevel());
		vo.setRegionName(baseRegion.getRegionName());
		List<BaseRegionVo> childs = findChilds(code);
		vo.setChilds(childs);
		return vo;
	}
	
	@Override
	public List<BaseRegionVo> findChilds(String code) {
		List<BaseRegionVo> regionVos=baseRegionMapper.findByParentCode(code);
		for (BaseRegionVo baseRegionVo : regionVos) {
			baseRegionVo.setChilds(baseRegionMapper.findByParentCode(baseRegionVo.getCode()));
		}
		return regionVos;
	}
}
